import utils.TreeNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/9 8:58
 * @description:
 */
public class Solution230 {
    public int m = 0;

    public int kthSmallest(TreeNode root, int k) {
        if (root == null) {
            return -1;
        }

        int left = kthSmallest(root.left, k);
        if (left != -1) {
            return left;
        }
        m++;
        if (m == k) {
            return root.val;
        }
        int right = kthSmallest(root.right, k);
        if (right != -1) {
            return right;
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution230 solution = new Solution230();
        TreeNode root = TreeNode.buildTree(new int[]{5, 3, 6, 2, 4, -1, -1, 1}, -1);
        System.out.println(solution.kthSmallest(root, 3));
    }
}
